<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-09 11:07:23
 * @LastEditTime: 2022-04-11 13:04:32
 * @LastEditors: yl
 * @Description: 生产单位
-->
<template>
  <BaseMainSlot
    v-loading="downLoading"
    element-loading-text="拼命下载中"
    element-loading-spinner="el-icon-loading"
    :show-tab="false"
    :show-search-box="false"
  >
    <template v-slot:btn-box>
      <ActionBar
        :page-no="612"
        :add-active="addActive"
        :del-active="delActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        @add="add"
        @cancel="cancel('formData')"
        @del="del"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label"></div>
          <div class="input"></div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <ZnFilterTable
        v-if="tableUpStauts === 1"
        :loadings="loading"
        :list-data="tableData"
        :prop-list="tableDataList"
        scope-no="manufacturerNo"
        @handleSelectionChange="handleSelectionChange"
        @rowClick="handleEdit"
        @tableUpdate="tableUpdate"
      >
        <template v-slot:url="{ row }">
          <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="previewImg(row)"
            >点击查看</el-button>
        </template>
        <template v-slot:operation="{ row }">
          <template>
            <el-button
              v-if="judgePermission(612, 'edit')"
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="edit"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-else-if="!judgePermission(612, 'edit')"
              disabled
              class="text-btn-cursor"
              type="text"
              size="small"
            >
              <i class="el-icon-edit-outline"></i> 修改
            </el-button>
            <el-button
              v-if="judgePermission(612, 'delete')"
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(row.manufacturerNo)"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
            <el-button
              v-else-if="!judgePermission(612, 'delete')"
              disabled
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
            >
              <i class="el-icon-delete"></i> 删除
            </el-button>
          </template>
        </template>
      </znfiltertable>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog id="tableColumnDialog" title="配置列" :visible.sync="dialogTableColumnVisible">
        <tableColumn v-if="dialogTableColumnVisible" ref="tableColumn" :table-column="allTableColumn" @cancelColumn="cancelColumn" />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="dialogTableColumnVisible = false">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="saveColumn">
            保 存
          </el-button>
        </div>
      </el-dialog>

      <el-dialog
        :title="titleText + livestock(textRouteName)"
        :visible.sync="dialogFormVisible"
        @close="cancel('formData')"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <template
            v-for="item,index in JSON.parse(livestock(JSON.stringify(formDataList)))"
          >
            <el-form-item
              v-if="item.show"
              :key="index"
              :label="item.name + ':'"
              :prop="item.porp"
              class="w50p"
            >
              <el-input
                v-if="item.type === 'input'"
                v-model="formData[ `${item.porp}`]"
                :placeholder="'请输入' + item.name"
                clearable
              />
            </el-form-item>
          </template>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >保 存</el-button>
        </div>
      </el-dialog>

    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import SearchBox from "@/components/SearchBox";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixins";
import { tableDataList, formDataList } from "./farmdataList";
import UploadPics from "@/components/UploadPics/upload-img-list.vue";
import salesManufacturer from "@/api/trace-source-web/base-infor-manage/manufacturer-management";
export default {
  name: "ManufacturerManagement",
  components: {
    ActionBar,
    Pagination,
    BaseMainSlot,
    SearchBox,
    UploadPics
  },
  mixins: [mymixin],
  data() {
    let validateNumber = (rule, value, callback) => {
      let patt = /^[+]?(\d+)$|^[+]?(\d+\.\d+)$/;
      if (value) {
        if (value !== "" && !patt.test(value)) {
          callback(new Error("值为大于0数"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      formStuats: true,
      validateNumber: validateNumber,
      groupFlag: true,
      // 员工姓名
      manufacturerNo: "",
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      selectedList: [],
      initialParameters: {},
      files: {},
      upLoading: false,
      downLoading: false,
      formData: {
        person: {}
      },
      tableDataList: tableDataList,
      formDataList: formDataList,
      fatherList: [],
      motherList: [],
      rules: {
        manufacturerName:[{ required: true, message: '请输入厂家名称', trigger: ['blur', 'change'] },],
        address:[{ required: true, message: '请输入厂家地址', trigger: ['blur', 'change'] },],
        contact: [
                    { required: true, message: '请输入厂家联系电话', trigger: ['blur', 'change'] },
                    {
                      pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{4}(\d{4})$/,
                      message: "请输入合法手机号/电话号",
                      trigger: "blur"
                    }]
      },
      topitemtypestu: true,
      searchObj: {},
      // 脱敏 start
      List: {}
      // 脱敏 end
    };
  },
  watch: {
  },
  created() {
  },
  mounted() {
    this.getTableDataList();
  },
  methods: {
    async SelectLinkage(e, x, nowChanceStau = 'add') {
      console.log("select linkage", e, nowChanceStau);
    },
    addPlus() {
    },
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchObj
        }
      };
      salesManufacturer.getList(params).then(res => {
        this.loading = false;
        let data = res.data;
        // 赋值总条数
        if (data) {
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = JSON.parse(this.livestock(JSON.stringify(data.content)));
          // console.log(this.tableData)
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    async handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify({ ...row, ...row.person }));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
    },
    handleSelectionChange(list) {
      this.selectedList = [];
      this.selectedList = list.map(item => item.manufacturerNo);
      if (this.selectedList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      salesManufacturer.delList(this.selectedList).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.search();
        } else {
          this.$message.warning(res.msg || "删除失败");
        }
      });
    },
    handleDelete(val) {
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          salesManufacturer.del(val).then(res => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.search();
            } else {
              this.$message.warning(res.msg || "删除失败");
            }
          });
        })
        .catch(() => {
          // this.$message({
          //   type: "info",
          //   message: "已取消删除"
          // });
        });
    },
    save(formData) {
      console.log(this.formData);
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            salesManufacturer.edit(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.search();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            salesManufacturer.add(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.search();
                this.$refs[formData].resetFields();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
// .staff-manage {
//   width: 100%;
//   height: 100%;
.formStuats {
  // position: absolute;
  // bottom: 80px;
  // right: 10px;
  display: inline-block;
  // width: 60px;
  // height: 32px;
  // line-height: 30px;
  // font-size: 16px;
  padding: 10px 20px;
  font-size: 14px;
  border-radius: 4px;
  // border-radius: 5px;
  box-shadow: 0 0 5px 0 #00000040;
  background-color: #1F74C7;
  color: #fff;
  margin-right: 10px;
  &:hover {
    background-color: #006f9128;
    border-color: #1F74C7;
    color: #06d4f8;
  }
}
::v-deep .el-form-item__content {
  // display: flex;
  justify-content: space-between;
}
::v-deep .el-form-item__label {
  min-width: 120px !important;
  text-align: end;
}
// }
// }
.DownExcel {
  margin-left: 20px;
  // background-color: #03bfef;
  // // border: 1px solid ;
}
::v-deep .pull-right {
  // height: 28px;
  // margin-left: 10px;
  .upload-demo {
    height: 100%;
    .el-upload {
      height: 100%;
      .el-button {
        height: 100%;
      }
    }
  }
}
</style>
